﻿using CPPEI.ETL.DataServiceInterface;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.DSImplement.MySql
{
    public class MySqlDtSerProxy : IDataServiceProxy
    {
        public TDataService GetSchema(DataServiceInfo info)
        {
            return null;
        }

        public TDataSource GetSchema(DataSourceInfo info)
        {
            return null;
        }

        public IData GetData(DataSourceInfo info, Dictionary<string, object> args)
        {
            string[] segments = info.Address.Split(new char[] { ',' });
            string server = segments[0];
            string database = segments[1];

            string connectionString = string.Format("Server={0};Database={1};Uid={2};Pwd={3}", server, database, info.AuthUser, info.AuthPassword);
            MySqlConnection connection = new MySqlConnection(connectionString);
            if(info.DataSourceType == DEF.SQLSourceType.Statement)
            {
                // sql statement
                MySqlCommand command = new MySqlCommand();
                command.Connection = connection;
                command.CommandText = info.DataSource;
                command.CommandType = System.Data.CommandType.Text;
                foreach (var argKey in args.Keys)
                {
                    MySqlParameter parameter = new MySqlParameter();
                    parameter.ParameterName = string.Format("@{0}", argKey);
                    parameter.Value = args[argKey];

                    command.Parameters.Add(parameter);
                }

                System.Data.DataTable dataTable = new System.Data.DataTable();
                MySqlDataAdapter adapter = new MySqlDataAdapter(command);

                adapter.Fill(dataTable);

                return new DataTableData(dataTable);
            }
            else if(info.DataSourceType == DEF.SQLSourceType.StoredProcedure)
            {
                // stored procedure
                MySqlCommand command = new MySqlCommand();
                command.Connection = connection;
                command.CommandText = info.DataSource;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                foreach (var argKey in args.Keys)
                {
                    MySqlParameter parameter = new MySqlParameter();
                    parameter.ParameterName = argKey;
                    parameter.Value = args[argKey];

                    command.Parameters.Add(parameter);
                }

                System.Data.DataTable dataTable = new System.Data.DataTable();
                MySqlDataAdapter adapter = new MySqlDataAdapter(command);

                adapter.Fill(dataTable);

                return new DataTableData(dataTable);
            }

            return null;
        }

        public List<string> Enumerate(DataServiceInfo info)
        {
            throw new NotImplementedException();
        }
    }
}
